package com.geek2.supermarket.service;

import com.geek2.supermarket.dao.UserMapper;
import com.geek2.supermarket.pojo.SmbmsRole;
import com.geek2.supermarket.pojo.SmbmsUser;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class UserServiceImpl implements UserService {
    //此处用set方法注入（配置文件）
    private UserMapper userMapper;
    private HashMap<String, Object> map;

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public List<SmbmsUser> queryAll(String userName, int roleId, int currentPageNo, int pageSize) {
        map = new HashMap<String, Object>(8);
        map.put("userName", userName);
        map.put("roleId", roleId);
        currentPageNo=(currentPageNo-1)*pageSize;
        map.put("currentPageNo", currentPageNo);
        map.put("pageSize", pageSize);
        return userMapper.queryAllUser(map);
    }

    @Override
    public SmbmsUser queryUser(String code, String id) {
        map = new HashMap<String, Object>(4);
        map.put("code", code);
        map.put("uid", id);
        return userMapper.queryUser(map);
    }

    @Override
    public List<SmbmsRole> queryRole() {
        return userMapper.queryRole();
    }

    @Override
    public boolean updateUserPwd(SmbmsUser smbmsUser) {
        map = new HashMap<String, Object>(4);
        map.put("userPassword", smbmsUser.getUserPassword());
        map.put("userId", smbmsUser.getId());
        int i = userMapper.updateUser(map);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean addUser(SmbmsUser smbmsUser) {

        int i = userMapper.addUser(smbmsUser);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean delUserById(String id) {
        if ((userMapper.delUser(id)) == 1) {
            return true;
        }
        return false;
    }

    @Override
    public int getUserCount(String username, int userRole) {
        map = new HashMap<String, Object>(4);
        map.put("username", username);
        map.put("roleId", userRole);
        return userMapper.getUserCount(map);
    }

    @Override
    public boolean updateUser(SmbmsUser smbmsUser) {

        map = new HashMap<String, Object>(16);
        map.put("userName", smbmsUser.getUserName());
        map.put("userId", smbmsUser.getId());
        map.put("gender", smbmsUser.getGender());
        map.put("birthday", smbmsUser.getBirthday());
        map.put("phone", smbmsUser.getPhone());
        map.put("address", smbmsUser.getAddress());
        map.put("role", smbmsUser.getUserRole());
        map.put("modifyDate", new Date());
        int i = userMapper.updateUser(map);
        if (i > 0) {
            return true;
        }
        return false;
    }
}
